package com.mswyt.utils.base;

import org.springframework.aop.support.AopUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @ClassName: MsProxyUtils
 * @Description: 动态代理相关工具类
 */
public class MsProxyUtils {
    //mp的mapper层接口代理
    private static final String MP_MAPPER_INTERFACE_PROXY = "mapperInterface";

    /**
     * 判断一个类是否是代理对象,包括jdk动态代理、aop代理，cglib代理
     *
     * @param object
     * @return
     * @author mashuai
     */

    public static <T> boolean isProxyClass(T object) {
        return Proxy.isProxyClass(object.getClass()) || AopUtils.isAopProxy(object) || AopUtils.isCglibProxy(object);
    }

    /**
     * 获取Mp实际需要代理对象的class
     *
     * @param object
     * @return
     * @author mashuai
     */

    public static Class<?> getMpProxyClass(Object object) throws Exception {
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(object);
        //获取目标对象实际需要代理的Class
        Class<?> mapperInterface1 = (Class<?>) MsReflexUtils.getClassFieldVal(invocationHandler, MP_MAPPER_INTERFACE_PROXY);
        return mapperInterface1;
    }
}
